
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;


public class KeyInputHandler extends KeyAdapter
{
	public boolean press_A, press_D, press_S, press_W, press_E, press_SPACE = false;
	
	// This flag ensures that dialogue is only advanced once per keypress.
	private boolean waitForERelease;
	
	private Game game;
	
	public KeyInputHandler(Game game)
	{
		this.game = game;
	}
	
	@Override
	public void keyReleased(KeyEvent e) 
	{
		if (e.getKeyChar() == KeyEvent.VK_A + 32)
		{
			press_A = false;
		}
		if (e.getKeyChar() == KeyEvent.VK_W + 32) 
		{
			press_W = false;	
			game.press_W = false;
		}
		if (e.getKeyChar() == KeyEvent.VK_D + 32) 
		{
			press_D = false;
		}
		if (e.getKeyChar() == KeyEvent.VK_S + 32) 
		{
			press_S = false;		
			game.press_S = false;
		}
		if (e.getKeyChar() == KeyEvent.VK_E + 32)
		{
			press_E = false;
		}
		if (e.getKeyChar() == KeyEvent.VK_SPACE)
		{
			press_SPACE = false;
		}
		
		if (press_A == false && press_D == false)
		{
			game.setVelocityX(0);
		} 
		/*if (press_S == false && press_W == false)
		{
			game.setVelocityY(5);
		}*/
		
		if (!press_E)
		{
			waitForERelease = false;
		}
		
	}
	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyChar() == KeyEvent.VK_A + 32)
		{
			press_A = true;
		}
		if (e.getKeyChar() == KeyEvent.VK_W + 32) 
		{
			press_W = true;		
			game.press_W = true;
		}
		if (e.getKeyChar() == KeyEvent.VK_D + 32) 
		{
			press_D = true;
		}
		if (e.getKeyChar() == KeyEvent.VK_S + 32) 
		{
			press_S = true;	
			game.press_S = true;
		}
		if (e.getKeyChar() == KeyEvent.VK_E + 32)
		{
			press_E = true;
		}
		if (e.getKeyChar() == KeyEvent.VK_SPACE)
		{
			press_SPACE = true;
			if (game.dialogue.isEnabled)
			{
				game.dialogue.advance();
			}
		}
		if(press_SPACE == true)
		{
			game.player.jump();
		}
		if (press_A == true)
		{
			game.setVelocityX(-8);
			game.player.flip = true;
		} 
		else if (press_D == true)
		{
			game.setVelocityX(8);
		}
		/*else if (press_W == true)
		{
			game.setVelocityY(-10);
		}*/
		
		if (press_E)
		{
			game.advanceText();
			waitForERelease = true;
		}
	}
}	